#!/usr/bin/python

# generate rules.py to be included
import os
if os.path.dirname(__file__): os.chdir(os.path.dirname(__file__))

outfile = open('rules.py', 'w')
outfile.write('# -*- coding: utf-8 -*-\n')
infile = open('rules.js')
outfile.write(infile.read())
infile.close()
outfile.close()

from rules import rules

import random

# auto unicode-ize the strings
for k in rules:
    rules[k] = [ s.decode('utf-8') for s in rules[k] ]

def substitute(tokenstr):
    ret = []
    for obj in tokenstr.split(' '):
        if obj[0] >= 'A' and obj[0] <= 'Z': # it is a grammar keyword
            ret.append( substitute(rules[obj][ random.randint(0, len(rules[obj]) - 1) ] ) )
        else:
            ret.append( obj ) # it is an actual chinese word

    return " ".join( ret )

print substitute('POEM')
